<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>江苏省事业编查询</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 50px;
        }
        .container {
            max-width: 800px;
            margin: auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 10px;
        }
        input, select {
            margin: 10px 0;
            padding: 8px;
            width: 100%;
            max-width: 300px;
        }
        .button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
        }
        .button:hover {
            background-color: #45a049;
        }
        table {
            width: 100%;
            margin-top: 20px;
            border-collapse: collapse;
        }
        th, td {
            padding: 10px;
            border: 1px solid #ddd;
            text-align: center;
        }
        th {
            background-color: #f2f2f2;
        }
        .pagination {
            margin-top: 20px;
        }
        .pagination button {
            padding: 5px 10px;
            margin: 0 5px;
            border: 1px solid #ddd;
            background-color: #f2f2f2;
            cursor: pointer;
        }
        .pagination button:hover {
            background-color: #ddd;
        }
        .pagination button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>江苏事业编查询</h1>
        <div>
            <input type="text" id="zhuguanname" placeholder="主管部门">
            <input type="text" id="danweiname" placeholder="名称">
            <input type="text" id="danweicode" placeholder="单位代码">
            <input type="text" id="gangweiname" placeholder="岗位名称">
            <input type="text" id="gangweileibie" placeholder="岗位类别">
            <input type="text" id="xueli" placeholder="学历">
            <input type="text" id="zhuanye" placeholder="专业">
            <input type="text" id="zhaopinduixiang" placeholder="拟招聘对象">
            <button class="button" onclick="searchData()" id="searchBtn">搜索</button>
             <button class="button" onclick="exportToExcel()" id="exportBtn">导出 Excel</button>
        </div>
        <table id="resultTable">
            <thead>
                <tr>
                    <th>主管部门</th>
                    <th>单位名称</th>
                    <th>单位代码</th>
                    <th>经费来源</th>
                    <th>岗位名称</th>
                    <th>岗位代码</th>
                    <th>岗位类别</th>
                    <th>岗位描述</th>
                    <th>拟招聘人数</th>
                    <th>开考比例</th>
                    <th>考察体检比例</th>
                    <th>学历</th>
                    <th>招聘对象</th>
                    <th>其他条件</th>
                    <th>招聘部门</th>
                    <th>其他说明</th>
                    <th>咨询电话</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
        <div class="pagination">
            <button id="prevPage" onclick="changePage(-1)" disabled>上一页</button>
            <span id="pageInfo">第 1 页</span>
            <button id="nextPage" onclick="changePage(1)" disabled>下一页</button>
        </div>
    </div>
    <script>
        let currentPage = 1;
        const pageSize = 10;
        let totalPages = 1;

        function searchData() {
            currentPage = 1; // 每次搜索时重置页数
            fetchData();
        }

        function fetchData() {
            const zhuguanname = document.getElementById("zhuguanname").value;
            const danweiname = document.getElementById("danweiname").value;
            const danweicode = document.getElementById("danweicode").value;
            const gangweiname = document.getElementById("gangweiname").value;
            const gangweileibie = document.getElementById("gangweileibie").value;
            const xueli = document.getElementById("xueli").value;
            const zhuanye = document.getElementById("zhuanye").value;
            const zhaopinduixiang = document.getElementById("zhaopinduixiang").value;
            const searchBtn = document.getElementById("searchBtn");
            searchBtn.disabled = true;

            fetch(`http://127.0.0.1:8000/tiaojisystem/searchcareerinfoapi?school=${school}&score_min=${scoreMin}&score_max=${scoreMax}&application_id=${applicationId}&tiaojicode=${tiaojicode}&page=${currentPage}&page_size=${pageSize}`)
                .then(response => response.json())
                .then(data => {
                    searchBtn.disabled = false;
                    if (data.success) {
                        totalPages = Math.ceil(data.totalRecords / pageSize);
                        renderTable(data.results);
                        updatePagination();
                    } else {
                        alert('数据获取失败');
                    }
                })
                .catch(error => {
                    searchBtn.disabled = false;
                    console.error('Error:', error);
                    alert('请求失败，请重试');
                });
        }

        function renderTable(records) {
            const tableBody = document.getElementById("resultTable").getElementsByTagName('tbody')[0];
            tableBody.innerHTML = '';
            if (records.length === 0) {
                const row = tableBody.insertRow();
                const cell = row.insertCell(0);
                cell.colSpan = 7;
                cell.textContent = "未找到符合条件的记录";
                return;
            }
            records.forEach(item => {
                const row = tableBody.insertRow();
                row.insertCell(0).textContent = item.yizhiyuanschool;
                row.insertCell(1).textContent = item.chushiscore;
                row.insertCell(2).textContent = item.yihziyuancode;
                row.insertCell(3).textContent = item.tiaojischool;
                row.insertCell(4).textContent = item.tiaojixueyuan;
                row.insertCell(5).textContent = item.tiaojizhuanyecode;
                row.insertCell(6).textContent = item.tiaojizhuanye;
            });
        }

       function updatePagination() {
            document.getElementById("pageInfo").textContent = `第 ${currentPage} 页 / 共 ${totalPages} 页`;
            document.getElementById("prevPage").disabled = currentPage === 1;
            document.getElementById("nextPage").disabled = currentPage >= totalPages;
        }


        function changePage(step) {
            if ((step === -1 && currentPage > 1) || (step === 1 && currentPage < totalPages)) {
                currentPage += step;
                fetchData();
            }
        }
        function exportToExcel() {
            const school = document.getElementById("school").value;
            const scoreMin = parseInt(document.getElementById("score_min").value) || 0;
            const scoreMax = parseInt(document.getElementById("score_max").value) || 9999;
            const applicationId = document.getElementById("application_id").value;
            const tiaojicode = document.getElementById("tiaojicode").value;
            const exportBtn = document.getElementById("exportBtn");
            exportBtn.disabled = true;

            let allData = []; // 存储所有数据
            let currentPagedemo = 1;
            let pageSizedemo = 10;

            alert("正在导出数据，请稍候...");

            // 使用递归逐页获取数据
            function fetchAllData() {
                fetch(`http://127.0.0.1:8000/tiaojisystem/searchapidemo3?school=${school}&score_min=${scoreMin}&score_max=${scoreMax}&application_id=${applicationId}&tiaojicode=${tiaojicode}&page=${currentPagedemo}&page_size=${pageSizedemo}`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        allData = allData.concat(data.results); // 将数据添加到 allData
                        // 如果当前页还不是最后一页，则继续获取下一页
                        if (data.results.length === pageSizedemo) {
                            currentPagedemo++;
                            fetchAllData(); // 递归获取下一页数据
                        } else {
                            // 所有数据都获取完后，开始导出
                            if (allData.length === 0) {
                                alert("没有数据可导出");
                                return;
                            }
                        let exportData = [["一志愿学校", "分数", "报考专业代码", "调剂学校", "调剂学院", "调剂专业代码", "调剂专业名称"]];
                        allData.forEach(item => {
                            exportData.push([item.yizhiyuanschool, item.chushiscore, item.yihziyuancode, item.tiaojischool, item.tiaojixueyuan, item.tiaojizhuanyecode, item.tiaojizhuanye]);
                        });
                        let ws = XLSX.utils.aoa_to_sheet(exportData);
                        let wb = XLSX.utils.book_new();
                        XLSX.utils.book_append_sheet(wb, ws, "调剂数据");
                        XLSX.writeFile(wb, "调剂数据.xlsx");
                    }
                } else {
                    alert('数据获取失败');
                }
            })
            .catch(error => {
                alert('请求失败，请重试');
            });
        }
        // 开始获取数据
        fetchAllData();
        }
    </script>
</body>
</html>